var HT_Selection = {
	_htmlSrc: null,
	
	setHtmlSrc: function(str){
    this._htmlSrc = str;
  },
  
	getHtmlSrc: function(){
		return this._htmlSrc;
	},
	
	getSelectionPos: function() {
		var oWindow = getBrowser().contentWindow;
		var selection = oWindow.getSelection();
		var strContent = selection + "";
		strContent = strContent.trim();
		if(strContent == ""){
			alert("Please select a fragment!");
			return null;
		}
		var range = selection.getRangeAt(0); 
		var startContainer = range.startContainer; 
		var endContainer = range.endContainer;
		var startOffset = range.startOffset; 
		var endOffset = range.endOffset; 
		var ancestorContainer = range.commonAncestorContainer;
		
		var doc = ancestorContainer.ownerDocument; 
		// let the ancestor be an element 
		if (ancestorContainer.nodeType == Node.TEXT_NODE || ancestorContainer.nodeType == Node.CDATA_SECTION_NODE) 
			ancestorContainer = ancestorContainer.parentNode;
		
		// for selectAll, let's use the entire document, including <html>...</html> 
		// @see DocumentViewerImpl::SelectAll() for how selectAll is implemented 
		try { 
				if(ancestorContainer == doc.body) 
					ancestorContainer = doc.documentElement; 
		} catch (e) { } 
		var startPath = this.getPath(ancestorContainer, startContainer); 
		var endPath = this.getPath(ancestorContainer, endContainer);
		var oldAncestorContainer = ancestorContainer;
		ancestorContainer = ancestorContainer.cloneNode(true);
		startContainer = ancestorContainer;
		endContainer = ancestorContainer;
		var canDrawSelection = ancestorContainer.hasChildNodes();
		if (canDrawSelection)
		{
				var i;
				for (i = startPath ? startPath.length - 1: - 1; i >= 0; i--)
				{
						startContainer = startContainer.childNodes.item(startPath[i]);
				}
				for (i = endPath ? endPath.length - 1: - 1; i >= 0; i--)
				{
						endContainer = endContainer.childNodes.item(endPath[i]);
				}
				var tmpNode;
				if (endContainer.nodeType == Node.TEXT_NODE || endContainer.nodeType ==
						Node.CDATA_SECTION_NODE)
				{
						if ((endOffset > 0 && endOffset < endContainer.data.length) ||
								!endContainer.parentNode || !endContainer.parentNode.parentNode)
								endContainer.insertData(endOffset, MARK_SELECTION_END);
						else
						{
								tmpNode = doc.createTextNode(MARK_SELECTION_END);
								endContainer = endContainer.parentNode;
								if (endOffset == 0)
										endContainer.parentNode.insertBefore(tmpNode, endContainer);
								else
										endContainer.parentNode.insertBefore(tmpNode,
												endContainer.nextSibling);
						}
				}
				else
				{
						tmpNode = doc.createTextNode(MARK_SELECTION_END);
						endContainer.insertBefore(tmpNode, endContainer.childNodes.item
								(endOffset));
				}
				if (startContainer.nodeType == Node.TEXT_NODE ||
						startContainer.nodeType == Node.CDATA_SECTION_NODE)
				{
						if ((startOffset > 0 && startOffset < startContainer.data.length)
								|| !startContainer.parentNode ||
								!startContainer.parentNode.parentNode || startContainer !=
								startContainer.parentNode.lastChild)
								startContainer.insertData(startOffset,
										MARK_SELECTION_START);
						else
						{
								tmpNode = doc.createTextNode(MARK_SELECTION_START);
								startContainer = startContainer.parentNode;
								if (startOffset == 0)
										startContainer.parentNode.insertBefore(tmpNode,
												startContainer);
								else
										startContainer.parentNode.insertBefore(tmpNode,
												startContainer.nextSibling);
						}
				}
				else
				{
						tmpNode = doc.createTextNode(MARK_SELECTION_START);
						startContainer.insertBefore(tmpNode, startContainer.childNodes.item
								(startOffset));
				}
		}
		
		var oldParentNode = oldAncestorContainer.parentNode;
		oldParentNode.replaceChild(ancestorContainer, oldAncestorContainer);
		var docHtml = doc.documentElement.innerHTML;
		debug("[selection.js] ========================html source to selection ==================",5);
		debug(docHtml,5);
		debug("[selection.js] ========================html source to selection ==================",5);
		var startPos = docHtml.indexOf(MARK_SELECTION_START);
		var endPos = docHtml.indexOf(MARK_SELECTION_END) - MARK_SELECTION_START.length;
		var len = endPos - startPos;
		debug("[selection.js] startPos:" + startPos + " ,endPos:" + endPos , 4);
		var startHtml = docHtml.substr(0, startPos);
		startPos = this.getRealLength(startHtml);
		endPos = startPos + len;
		debug("[selection.js] %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%", 4);
		debug("startPos: " + startPos + ", endPos:" + endPos, 4);
		debug("[selection.js] %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%", 4);
		debug("[selection.js] current selection:" + startPos + "," + endPos + "," + strContent, 4);
		//oldParentNode.replaceChild(oldAncestorContainer, ancestorContainer);
		
		//var selectionPos = new Array(startPos, endPos);
		var selectionObject = {};
		selectionObject.startPos = startPos;
		selectionObject.endPos = endPos;
		selectionObject.strContent = strContent;
		
		return selectionObject;
	},
	
	getPath: function(ancestor, node)
	{
			var n = node;
			var p = n.parentNode;
			if (n == ancestor || !p)
					return null;
			var path = new Array();
			if (!path)
					return null;
			do
			{
					for (var i = 0; i < p.childNodes.length; i++)
					{
							if (p.childNodes.item(i) == n)
							{
									path.push(i);
									break;
							}
					}
					n = p;
					p = n.parentNode;
			}
			while (n != ancestor && p);
			return path;
	},
	
	highLightByIndexOf: function(selectionText, colorIndex){
		highlightStartTag = "<font style='background-color:" + COLORS[colorIndex] + ";'>";
		highlightEndTag = "</font>";
		

		var result = "";
		var lcSelectionText = selectionText.toLowerCase();
		while(selectionText.length > 0){

			var startPos = selectionText.lastIndexOf("<");
			var endPos = selectionText.lastIndexOf(">");
			if (endPos >= startPos) {
				
				htmlTag = selectionText.substring(startPos, endPos + 1);
				if(htmlTag.toLowerCase().indexOf("/script") > 0){
					startPos = lcSelectionText.lastIndexOf("<script");
					endPos = lcSelectionText.lastIndexOf("/script>") + 9;
					htmlTag = selectionText.substring(startPos, endPos);
				}
				if(htmlTag.toLowerCase().indexOf("/textarea") > 0){
					startPos = lcSelectionText.lastIndexOf("<textarea");
					endPos = lcSelectionText.lastIndexOf("/textarea>") + 11;
					htmlTag = selectionText.substring(startPos, endPos);
				}
				if(selectionText.length > endPos+1)
							result = htmlTag + highlightStartTag + selectionText.substring(endPos + 1) + highlightEndTag + result;
						else
					result = htmlTag + result;
				selectionText = selectionText.substring(0, startPos);
						lcSelectionText = selectionText.toLowerCase();
					}
			else{
				if(startPos > 0)
					result = highlightStartTag + selectionText.substring(endPos+1, startPos) + highlightEndTag + selectionText.substring(startPos) + result;
				else
					result = highlightStartTag + selectionText.substring(endPos + 1) + highlightEndTag + result;
				selectionText = selectionText.substring(0, endPos+1);
						lcSelectionText = selectionText.toLowerCase();
			}
		}
		return result;
	},
	
	getRealLength: function(str){
		debug("[selection.js : getRealLength] str:" + str ,4);
		var lastPos = str.lastIndexOf("</hypertopic>");
		if(lastPos < 0){
			debug("[selection.js : getRealLength] no fragment found", 4);
			return str.length;
		}
		lastPos += "</hypertopic>".length;
		var myRe= /<hypertopic type="end" id="ht_fragment_end_([0-9]+)_([0-9]+)"><\/hypertopic>/ig;
		var endPos = 0;
		while ((myArray = myRe.exec(str)) != null){
			endPos = myArray [2];
		}
		try { endPos = parseInt(endPos); } catch(ex) {}
		debug("[selection.js : getRealLength] endPos:" + endPos ,4);
		var len = endPos + (str.length - lastPos);
		debug("[selection.js : getRealLength] len:" + len ,4);
		return len;
	}
};